using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class 背景运动补偿 : MonoBehaviour
{
    public float parallaxScale = 0.65f;//总体层=移动速度
    public float frameScale = 0.4f;//点个层移动速度
    public float smooth = 4f;//背景滚动移动速度
    [SerializeField]
    Transform[] backgrounds;
    Vector3 camPrePos;
    Transform camTran;
    // Start is called before the first frame update
    private void Awake()
    {
        camTran= Camera.main.transform;
        //camPrePos= Camera.main.transform.position;两个方法
    }

    void Start()
    {
        camPrePos= camTran.position;
    }


    // Update is called once per frame
    void Update()
    {
        float fparallaxx = parallaxScale * (camPrePos.x - camTran.position.x);
        float fparallaxy = parallaxScale * (camPrePos.y - camTran.position.y);
        for (int i=0; i<backgrounds.Length; i++)
        {
            //层越后面移动越大
            float newX = backgrounds[i].position.x + fparallaxx * (1 + frameScale * i);
            float newY = backgrounds[i].position.y + fparallaxy * (1 + frameScale * i);
            Vector3 newPos = new Vector3(newX, newY, backgrounds[i].position.z);
            backgrounds[i].position = Vector3.Lerp(backgrounds[i].position, newPos, smooth*Time.deltaTime);//Time.deltaTime每一帧所用时间
        }
        camPrePos= camTran.position;
    }
}
